/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import hibentities.Purchase;
import hibentities.PurchaseDetail;
import hibernatedaos.ProductHibernateDao;
import hibernatedaos.PurchaseHibernateDao;
import hibernatedaos.UserHibernateDao;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Zakup produktów przez Hibernate
 * @author piotrek
 */
@Controller
@RequestMapping(value="/hibernate")
public class PurchaseHibernateController {

	private final PurchaseHibernateDao purchaseDao;
	private ProductHibernateDao productDao;
	private UserHibernateDao userDao;
	
	@Autowired
    public PurchaseHibernateController( PurchaseHibernateDao purchaseDao, ProductHibernateDao productDao, UserHibernateDao userDao) {
		this.purchaseDao = purchaseDao;
		this.productDao		= productDao;
		this.userDao		= userDao;
	}

	/**
	 * Ok
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/{id}/purchase", method=RequestMethod.GET)
    protected String showPurchaseHibernate( @PathVariable("id") String id,
            Model model) throws Exception {

		model.addAttribute( "id", id );

		Purchase purchase = purchaseDao.getPurchase( Long.parseLong(id) );

		model.addAttribute( "purchase", purchase );

//System.out.println(purchase.getPurchaseDetails().iterator().next().getIdPurchaseDetail());

		return "purchase";
    }

	/**
	 * Zakup
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/purchase/add")
	protected String savePurchase( HttpServletRequest request,
            HttpServletResponse response){

		Purchase p = new Purchase();

		p.setCost( Math.random()*100);
		p.setCustomer(userDao.getUser( Long.parseLong("458") ) );
		p.setPurchaseDate( new Date() );

		//przetwarzanie tablicy z danymi purchase details
		Set<PurchaseDetail> purchaseDetails = new HashSet<PurchaseDetail>();

		//petla generujaca detale zakupu
		for(int i=0;i<3;i++){
			PurchaseDetail pd = new PurchaseDetail();
			pd.setProduct(productDao.getProduct( Long.parseLong("3")));//mozelosowo
			pd.setAmount( (int)(Math.random()*100) );
			pd.setPurchase(p);
			purchaseDetails.add(pd);

		}
		p.setPurchaseDetails( purchaseDetails );

		purchaseDao.addPurchase( p );

		return "success";

	}

	/**
	 * Zakup z dostawa
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/purchase/addWithShipment")
	protected String savePurchaseWithShipment( HttpServletRequest request,
            HttpServletResponse response){

		Purchase p = new Purchase();

		p.setCost( Math.random()*100);
		p.setCustomer(userDao.getUser( Long.parseLong("458") ) );
		p.setPurchaseDate( new Date() );

		//przetwarzanie tablicy z danymi purchase details
		Set<PurchaseDetail> purchaseDetails = new HashSet<PurchaseDetail>();

		//petla generujaca detale zakupu
		for(int i=0;i<3;i++){
			PurchaseDetail pd = new PurchaseDetail();
			pd.setProduct(productDao.getProduct( Long.parseLong("3")));//mozelosowo
			pd.setAmount( (int)(Math.random()*100) );
			pd.setPurchase(p);
			purchaseDetails.add(pd);

		}
		p.setPurchaseDetails( purchaseDetails );

		purchaseDao.addPurchaseWithShipment( p );

		return "success";

	}

	@RequestMapping(value="/{id}/purchase/update")
    protected String updatePurchaseHibernate( @PathVariable("id") String id,
            Model model) throws Exception {

		Purchase purchase = purchaseDao.getPurchase( Long.parseLong(id) );

		purchase.setCost( Math.random()*100 );

		/**
		 * Pobierz jedne z detali i zmien jedna z wartosci
		 */
		 for( PurchaseDetail pd: purchase.getPurchaseDetails() ){

			 pd.setAmount( (int)(Math.random()*100) );

		 }

		purchaseDao.updatePurchase(purchase);

		return "success";
		
	}


	@RequestMapping(value="/{id}/transactional/purchase/update")
    protected String updateTransactionalPurchaseHibernate( @PathVariable("id") String id,
            Model model) throws Exception {

		Purchase purchase = purchaseDao.getPurchase( Long.parseLong(id) );

		purchase.setCost( Math.random()*100 );

		/**
		 * Pobierz jedne z detali i zmien jedna z wartosci
		 */
		 for( PurchaseDetail pd: purchase.getPurchaseDetails() ){

			 pd.setAmount( (int)(Math.random()*100) );

		 }

		purchaseDao.updateTransactionalPurchase(purchase);

		return "success";

	}
}
